<?php
/**
 * Created by PhpStorm.
 * User: duanshuai
 * Date: 2018/3/22
 * Time: 下午10:22
 */

namespace app\index\controller;
use think\Db;
use think\request;
use app\index\model;
class User extends Base
{
    //登陆
    public function login() {

        $request = request()->param();

//        return $data;
        if ($this->checkParamsNoToken($request,['count','password'])){
            $count = $request['count'];
            $password = $request['password'];
            $data = [];
            $user =  model\User::get(['count'=>$count]);




            if ($user){
                $new_pas =  $user->password;
                if ($new_pas == $password){
                    //生成token 和 token_s 还有过期时间
                   $time_out =  Db::name('config')->where(['key'=>'time_out'])->value('value');
                    $para = $this -> crateTokenAndSctoken($count,$password);
                    $para['create_time'] = time();
                    $para['out_time'] = $para['create_time']+$time_out;
                    $para['count'] = $count;

                    $id =  Db::name('login')->insertGetId($para);
                    $user->save(['token_id'=>$id]);
                    unset($para['create_time'],$para['out_time'],$para['count']);

                    $data = $this->dataFormate_0('登陆成功',$para);


                }else{
                    $data = $this->dataFormate_1('密码错误');
                }

            }else{
                $data = $this->dataFormate_1('账号不存在');
            }

            return $data;


        }else{
            return $this->dataFormate_1('缺少参数');

        }


        //连接数据库,查询用户是否存在 级 密码是否正确
//       $result = Db::name('user') -> where('count',$count) ->select();
//        if ($result) {//账号存在
//            foreach ($result as $val) {
//               // var_dump($val);
//                if ($val['password']==$password) {
////                    var_dump(1);
//                    $data= ['status'=>0,'statusCode'=>'登陆成功','data'=>$val];
//                }else {
////                    var_dump(2);
//                    $data= ['status'=>1,'statusCode'=>'密码错误','data'=>[]];
//                }
//            }
//        }else{
////            var_dump('账号不存在');
//            $data= ['status'=>1,'statusCode'=>'账号不存在','data'=>[]];
//
//        }
//        return $data;

     }


     //注册
    public function regist() {

         $request = request();


        if ($this->checkParamsNoToken($request->param(),['count','password'])){
            $data = [];
            $count = $request -> param('count');
            $password = $request ->param('password');
            $name = $request -> param('name');
            $phone = $request ->param('phone');
            $age = $request ->param('age');

            $user = new model\User();

            if ($user -> isUserRegiste($count)) {
                //已经注册
                $data = $this->dataFormate_1('账号已经注册');
            }else{
                $user->registeOrUpdateCount(['count'=>$count,'password'=>$password,'name'=>$name,'phone'=>$phone,'age'=>$age]);
                $data = $this->dataFormate_0('账号注册成功');
        }
            return $data;


        }else{
            return $this->dataFormate_1('缺少参数');
        }





     }


    /**
     * @Notes:更新个人信息
     * @User: 段帅
     * @Time: 2018/7/4  上午11:18
     */
    public function update(){
        $request = request()->param();
        $data = [];
        $result = $this->checkParamsWithToken($request);

        if ($result['result']){
        $user = new model\User();
            //已经注册
            unset($result['result']);
            $user ->registeOrUpdateCount($result);
            $data = ['status'=>0,'statusCode'=>'更新成功','data'=>[]];

        }else{
            $data = $this->dataFormate_1('认证失败',$result);
        }
        return $data;
    }

    /**
     * Notes:更改密码
     * User: 段帅
     * Time: 2019/6/10  3:59 PM(摁tab)
     */
    public function updatePassWord(){
        $request = request()->param();
        $data = [];
        $result = $this->checkParamsWithToken($request);

        if ($result['result']){
            $user = new model\User();
            //已经注册
            $user =  $user::getByCount($result['count']);
            $pass = $user -> password;
            if ($pass == $request['old_pass']) {
                $user -> save(["password"=>$request['new_pass']]);
                $data = ['status'=>0,'statusCode'=>'更新成功','data'=>[]];
            }else {
                $data = $this->dataFormate_1('原密码错误',[]);
            }




        }else{
            $data = $this->dataFormate_1('认证失败',$result);
        }
        return $data;
    }




    //获取用户信息
    public  function getUserMessage(){
        $request = request()->param();
        $result = $this->checkParamsWithToken($request);
        if ($result['result']){
            unset($result['result']);
            $user = new model\User();
            $data =  $user->findWhitCount($result['count']);

        }else{
            $data = $this->dataFormate_1('认证失败',$result);
        }



        return $data;

    }


    /**
     * @Notes: 意见反馈
     * @User: 段帅
     * @Time: date_ds
     * @return array
     */
    public function feedback(){

        $request = request()->param();
        $result = $this->checkParamsWithToken($request,['title','content']);
        if ($result['result']){
            unset($result['result']);
            $user = new model\Feedback();
            $user->saveFeedback($result);
            $data =  $this->dataFormate_0('成功');
        }else{
            $data = $this->dataFormate_1('认证失败',$result);
        }



        return $data;



    }


    /**
     * @Notes:上传头像
     * @User: 段帅
     * @Time: date_ds
     */
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $request = request()->param();
        $result = $this->checkParamsWithToken($request);

        if ($result['result']){

            $file = request()->file('image');

            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
//                echo $info->getExtension();
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg

                    //获取host
                    $host =  Db::name('config')->where(['key'=>'host'])->value('value');
                    $adr = $host.'uploads/'.$info->getSaveName();
                    $user = new model\User();

                    $user->registeOrUpdateCount(['image'=>$adr,'count'=>$result['count']]);
                    return $this->dataFormate_0('上传成功',['adr'=>$adr]);

//                    echo $info->getSaveName();
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                    echo $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }




        }else{
            $data = $this->dataFormate_1('认证失败',$result);
        }



    }





}